<?php
	autoLoader();
	
	$objPatient 	= new Patients();
	$objCasePaper 	= new CasePapers();
	$objChargeTypes = new ChargeTypes();
	
	if( true == isset( $_GET['patient_id'] ) ) {
		$objPatient = $objPatient->fetchPatientsById( $_GET['patient_id'] );
	}
	
	if( true == isset( $_GET['case_paper_id'] ) && true == valStr( $_GET['case_paper_id'] ) ) {
		$objCasePaper = $objCasePaper->fetchCasePapersById( $_GET['case_paper_id'] );
	}

	if( true == valObj( $objCasePaper, 'BaseCasePapers' ) ) {
		$objBlood 	= new Bloods();
		$strSql 	= 'SELECT * FROM bloods WHERE case_paper_id = ' . $objCasePaper->getId() . ';';
		
		$arrobjBloods = $objBlood->fetchSql( $strSql );
	}
	
	$strSql = 'SELECT * FROM charge_types WHERE charge_catagory_id = ' . ChargeCatagoriesType::CHARGE_CATAGORY_LAB . ';';
	$arrobjChargeTypes = $objChargeTypes->fetchSql( $strSql );
	
	if( true == isset( $_GET['blood_id'] ) && true == valStr( $_GET['blood_id'] ) ) {
		$objBlood = new Bloods();
		$objLoadedBlood = $objBlood->fetchBloodsById( $_GET['blood_id'] );
	}
	
	if( true == valObj( $objLoadedBlood, 'BaseBloods' ) ) {
		$objCharge = new Charges();
		$objCharge = $objCharge->fetchChargesById( $objLoadedBlood->getChargeId() );
	}
	
?>
<div id="loaded_blood">
<fieldset style="border:1px solid gray; width:100%;">
<legend style="padding:0.2em 0.5em;border:1px solid gray;color:#2C6193;font-size:13px;text-align:left;font-weight:bold;margin-bottom: 1px;">Blood Report</legend>
	
	<div style="padding: 2px;display: none;margin:5px 5px 0px 5px;" class="alert alert-success" id="blood_success_display">
		<img alt="blood_insert_success Found with info.." src="images/ok.png" width="20" height="20">
		<label id='blood_insert_success'></label>
	</div>
	
	<div style="padding: 2px;display: none;margin:5px 5px 0px 5px;" class="alert alert-danger" id="blood_failed_display">
		<img alt="Patient Found with info.." src="images/close.png" width="20" height="20">
		<label id='blood_insert_failed'></label>
	</div>
	
	<div style="clear: both;"></div>
	
	<div id="left_side_continer" style="float: left; border-style: solid; border-color: #2C6193; width: 22%; height: 100px; background-color: #CCCCFF;">
		<?php
			if( true == valArr( $arrobjBloods ) ) {
				echo '<ul id="list-nav">';
				foreach( $arrobjBloods as $objBlood ) {
					echo '<a onclick="loadBloodData(' . $objBlood->getId() . ')"><li>' . $objBlood->getId() . '->' . date( 'd-M-Y', strtotime( $objBlood->getDateOfExamination() ) ) . '</li></a>';
				}
				echo '</ul>';
			} else {
				echo "<label>No Blood Report Avaliable</label>";
			}
		?>
	</div>
	
	<div style="float: right; width: 76%;">
		<form action="" method="post" enctype="multipart/form-data" id="blood_insert_form">
			<div>
				<div style="width: 50%; float: left;">
					Charges<label style="color: red;">*</label>
					<select name="charge_applied" id="charge_applied" onchange="calculateAmount();">
						<option value="">Select Charge</option>
						<?php
							if( true == valArr( $arrobjChargeTypes ) ) {
								foreach( $arrobjChargeTypes as $objChargeType ) {
									$intChargeAmount 	= ( true == valObj( $objPatient, 'BasePatients' ) && PatientType::PATIENT_CB == $objPatient->getPatientTypeId() ) ? $objChargeType->getCbArea() : $objChargeType->getOutOfCa();
									$strSelected 		= ( ( true == valObj( $objLoadedBlood, 'BaseBloods' ) && true == valObj( $objCharge, 'BaseCharges' ) && $objCharge->getChargeTypeId() == $objChargeType->getId() ) ? 'selected="selected"' : NULL );
									echo '<option id="' . $intChargeAmount . '" value="' . $objChargeType->getId() . '"' . $strSelected . '>' . $objChargeType->getName() . '</option>';
								}
							}
						?>
					</select>
				</div>
				<div><?php $intChargeAmount = ( ( true == valObj( $objCharge, 'BaseCharges' ) ) ? $objCharge->getRate() : 0 ); ?> Charge Applied: <label id="selectd_charge_amount"><?php echo $intChargeAmount; ?></label></div>
				<div style="clear: both;"></div>
			</div>
			<div style="clear: both;"></div>
			
			<div>
			<?php
				$intHB = $intTotalWbcs = $intDifferentialCount = $intPolymorphs = $intEosinophils = $intBasophi = $intLymphocytes = $intMonocytes = $intMalarialParasite = $intEsrInListHour = $intSpecialFindings = $intBT = $intCT = $strRemark = $intChargeId = NULL;
				
				if( true == valObj( $objLoadedBlood, 'BaseBloods' ) ) {
					$intHB 					= $objLoadedBlood->getHb();
					$intTotalWbcs			= $objLoadedBlood->getTotalWbcs();
					$intDifferentialCount	= $objLoadedBlood->getDifferentialCount();
					$intPolymorphs			= $objLoadedBlood->getPolymorphs();
					$intEosinophils			= $objLoadedBlood->getEosinophils();
					$intBasophi				= $objLoadedBlood->getBasophi();
					$intLymphocytes			= $objLoadedBlood->getLymphocytes();
					$intMonocytes			= $objLoadedBlood->getMonocytes();
					$intMalarialParasite	= $objLoadedBlood->getMalarialParasite();
					$intEsrInListHour		= $objLoadedBlood->getEsrInListHour();
					$intSpecialFindings		= $objLoadedBlood->getSpecialFindings();
					$intBT					= $objLoadedBlood->getBt();
					$intCT					= $objLoadedBlood->getCt();
					
					$intChargeId 			= $objLoadedBlood->getChargeId();

					$strRemark 				= $objLoadedBlood->getRemark();
				} 
			?>
				<table style="border-style: solid;" class="table table-striped table-bordered table-hover dataTable no-footer">
					<tr>
						<th>Particular Name</th>
						<th>Test Result</th>
						<th>Valuations</th>
						<th>Values</th>
					</tr>
					<tr>
						<td style="line-height: 1; padding: 2px;">H . B.</td>
						<td style="line-height: 1; padding: 2px;"><input type="text" size="5" value="<?php echo $intHB; ?>" name="hb" id="hb" /></td>
						<td style="line-height: 1; padding: 2px;">Grams<br/>Colour</td>
						<td style="line-height: 1; padding: 2px;"><input class="pc_data" type="text" size="5" value="" name="gram_color_index" id="gram_color_index" /></td>
					</tr>
					<tr>
						<td style="line-height: 1; padding: 2px;">Total W B Cs</td>
						<td style="line-height: 1; padding: 2px;"><input type="text" size="5" value="<?php echo $intTotalWbcs; ?>" name="wbc" id="wbc" /></td>
						<td style="line-height: 1; padding: 2px;">Per C m m</td>
						<td style="line-height: 1; padding: 2px;"><input class="pc_data" type="text" size="5" value="" name="wbc_pc" id="wbc_pc" /></td>
					</tr>
					<tr>
						<td style="line-height: 1; padding: 2px;">Differential Count</td>
						<td style="line-height: 1; padding: 2px;"><input type="text" size="5" value="<?php echo $intDifferentialCount; ?>" name="differential_count" id="differential_count" /></td>
						<td style="line-height: 1; padding: 2px;">&nbsp;</td>
						<td style="line-height: 1; padding: 2px;">&nbsp;</td>
					</tr>
					<tr>
						<td style="line-height: 1; padding: 2px;">Polymorphs</td>
						<td style="line-height: 1; padding: 2px;"><input type="text" size="5" value="<?php echo $intPolymorphs; ?>" name="polymorphs" id="polymorphs" /></td>
						<td style="line-height: 1; padding: 2px;">PC</td>
						<td style="line-height: 1; padding: 2px;"><input class="pc_data" type="text" size="5" value="" name="polymorphs_pc" id="polymorphs_pc" /></td>
					</tr>
					<tr>
						<td style="line-height: 1; padding: 2px;">Eosinophils</td>
						<td style="line-height: 1; padding: 2px;"><input type="text" size="5" value="<?php echo $intEosinophils; ?>" name="eosinophils" id="eosinophils" /></td>
						<td style="line-height: 1; padding: 2px;">PC</td>
						<td style="line-height: 1; padding: 2px;"><input class="pc_data" type="text" size="5" value="" name="eosinophils_pc" id="eosinophils_pc" /></td>
					</tr>
					<tr>
						<td style="line-height: 1; padding: 2px;">Basophi</td>
						<td style="line-height: 1; padding: 2px;"><input type="text" size="5" value="<?php echo $intBasophi; ?>" name="basophi" id="basophi" /></td>
						<td style="line-height: 1; padding: 2px;">PC</td>
						<td style="line-height: 1; padding: 2px;"><input class="pc_data" type="text" size="5" value="" name="basophi_pc" id="basophi_pc" /></td>
					</tr>
					<tr>
						<td style="line-height: 1; padding: 2px;">Lymphocytes</td>
						<td style="line-height: 1; padding: 2px;"><input type="text" size="5" value="<?php echo $intLymphocytes; ?>" name="lymphocytes" id="lymphocytes" /></td>
						<td style="line-height: 1; padding: 2px;">PC</td>
						<td style="line-height: 1; padding: 2px;"><input class="pc_data" type="text" size="5" value="" name="lymphocytes_pc" id="lymphocytes_pc" /></td>
					</tr>
					<tr>
						<td style="line-height: 1; padding: 2px;">Monocytes</td>
						<td style="line-height: 1; padding: 2px;"><input type="text" size="5" value="<?php echo $intMonocytes; ?>" name="monocytes" id="monocytes" /></td>
						<td style="line-height: 1; padding: 2px;">PC</td>
						<td style="line-height: 1; padding: 2px;"><input class="pc_data" type="text" size="5" value="" name="monocytes_pc" id="monocytes_pc" /></td>
					</tr>
					<tr>
						<td style="line-height: 1; padding: 2px;">Malarial Parasite</td>
						<td style="line-height: 1; padding: 2px;"><input type="text" size="5" value="<?php echo $intMalarialParasite; ?>" name="malarial_parasite" id="malarial_parasite" /></td>
						<td style="line-height: 1; padding: 2px;">&nbsp;</td>
						<td style="line-height: 1; padding: 2px;">&nbsp;</td>
					</tr>
					<tr>
						<td style="line-height: 1; padding: 2px;">E S R In 1st Hour</td>
						<td style="line-height: 1; padding: 2px;"><input type="text" size="5" value="<?php echo $intEsrInListHour; ?>" name="esr_in_hour" id="esr_in_hour" /></td>
						<td style="line-height: 1; padding: 2px;">m m</td>
						<td style="line-height: 1; padding: 2px;"><input class="pc_data" type="text" size="5" value="" name="esr_in_hour_pc" id="esr_in_hour_pc" /></td>
					</tr>
					<tr>
						<td style="line-height: 1; padding: 2px;">Special Findings</td>
						<td style="line-height: 1; padding: 2px;"><input type="text" size="5" value="<?php echo $intSpecialFindings; ?>" name="special_findings" id="special_findings" /></td>
						<td style="line-height: 1; padding: 2px;">&nbsp;</td>
						<td style="line-height: 1; padding: 2px;">&nbsp;</td>
					</tr>
					<tr>
						<td style="line-height: 1; padding: 2px;">B. T.</td>
						<td style="line-height: 1; padding: 2px;"><input type="text" size="5" value="<?php echo $intBT; ?>" name="bt" id="bt" /></td>
						<td style="line-height: 1; padding: 2px;">&nbsp;</td>
						<td style="line-height: 1; padding: 2px;">&nbsp;</td>
					</tr>
					<tr>
						<td style="line-height: 1; padding: 2px;">C. T.</td>
						<td style="line-height: 1; padding: 2px;"><input type="text" size="5" value="<?php echo $intCT; ?>" name="ct" id="ct" /></td>
						<td style="line-height: 1; padding: 2px;">&nbsp;</td>
						<td style="line-height: 1; padding: 2px;">&nbsp;</td>
					</tr>
				</table>
			</div>
			
			<div>
				<div style="float: left;">Remark (optional)</div>
				<div style="margin: 5px; padding-left: 10px; float: left;">
					<textarea rows="3" cols="20" name="remark"><?php echo $strRemark; ?></textarea>
				</div>
				<div style="clear: both;"></div>
			</div>
			
			<div style="margin-top: 15px;">
				<div>
					<?php
						if( true == isset( $_GET['blood_id'] ) && true == valStr( $_GET['blood_id'] ) ) {
							echo "<input type=hidden name=operation value=update />";
							echo "<input type=button value=UPDATE id=submit name=submit onclick='insertBloodForm();' />";
							echo "<input type=button value=Add New id=add_new name=add_new onclick='addNewXRayReport();' />";
							echo "<input type=button value=Cancel id=cancel name=cancel />";
						} else {
							echo "<input type=hidden name=operation value=insert />";
							echo "<input type=button value=SUBMIT id=Submit name=submit onclick='insertBloodForm();' />";
							echo "<input type=button value=Cancel id=cancel name=cancel onclick='closeXray();' />";
						}
					?>
				</div>
				<?php if( true == valStr( $intChargeId ) ) {?><input type="hidden" name="charge_id" value="<?php echo $intChargeId; ?>" /><?php } ?>
				<input type="hidden" name="blood_id" value="<?php echo ( true == isset( $_GET['blood_id'] ) ) ? $_GET['blood_id'] : NULL; ?>" />
				<input type="hidden" name="patient_id" value="<?php echo ( true == isset( $_GET['patient_id'] ) ) ? $_GET['patient_id'] : NULL; ?>" />
				<input type="hidden" name="case_paper_id" value="<?php echo ( true == isset( $_GET['case_paper_id'] ) ) ? $_GET['case_paper_id'] : NULL; ?>" />
			</div>
		</form>
	</div>
</fieldset>
</div>
<script>
	function insertBloodForm() {
		var intPatientId =	$('#patient_details').val();
		var postData = $('#blood_insert_form').serializeArray();
	
		strUrl = "modules.php?file=insert_blood_form&type=insert";
	
		$.ajax({
			url: strUrl,
			type: "post",
			data: postData,
			success: function( returnData ) {
				strCustomerData = returnData;
				if( 1 == strCustomerData ) {
					$('#blood_success_display').css( 'display', 'block' );

					$('#blood_failed_display').css( 'display', 'none' );

					loadAllBloodData();
				} else {
					$('#blood_failed_display').css( 'display', 'block' );
					$('#blood_insert_failed').html( 'Failed to insert Blood details' );

					$('#blood_success_display').css( 'display', 'none' );
				}
			},
			error:function(){
				alert( "! Patient not found with provided information, Please check entered data of patient." );
			}
		});
	}

	function loadBloodData( intBloodId ) {
		var intPatientId 	=	$('#patient_id').val();
		var intCasePaperId 	=	$('#case_paper_id').val();
		
		var strUrl = "modules.php?file=blood_test&type=forms&blood_id=" + intBloodId + '&patient_id=' + intPatientId + '&case_paper_id=' + intCasePaperId;

		$.ajax({
			url: strUrl,
			type: "post",
			data: 'blood_id=' + intBloodId,
			success: function( returnData ) {
				strBloodData = returnData;
				
				if( strBloodData != '' ) {
					$('#loaded_blood').html( strBloodData );
				}
			},
			error:function(){
				alert( "! Patient not found with provided information, Please check entered data of patient." );
			}
		});
	}

	function addNewXRayReport() {
		var intPatientId 	=	$('#patient_id').val();
		var intCasePaperId 	=	$('#case_paper_id').val();
		
		var strUrl = 'modules.php?file=blood_test&type=forms&patient_id=' + intPatientId + '&case_paper_id=' + intCasePaperId;

		$.ajax({
			url: strUrl,
			type: "post",
			data: '',
			success: function( returnData ) {
				strXrayData = returnData;
				
				if( strXrayData != '' ) {
					$('#loaded_blood').html( strXrayData );
				}
			},
			error:function(){
				alert( "! Patient not found with provided information, Please check entered data of patient." );
			}
		});
	}

	function calculateAmount() {
		var intChargeAmount = $( "#charge_applied option:selected" ).attr( 'id' );
		$('#selectd_charge_amount' ).html( intChargeAmount );
	}

	function loadAllBloodData() {
		var intPatientId 	=	$('#patient_id').val();
		var intCasePaperId 	=	$('#case_paper_id').val();

		$strUrl = "modules.php?file=blood_test&type=forms&patient_id=" + intPatientId + "&case_paper_id=" + intCasePaperId;
	
		$.ajax({
			url: $strUrl,
			type: "post",
			data: "patient_data=" + intPatientId,
			beforeSend: function() {
				$("#wait").css("display","block");
				$('.col-lg-10').css("display","none");
			},
			success: function( returnData ) {
				strCustomerData = returnData;
				$('#blood_from' ). html( strCustomerData );
				$( "#blood_from" ).show( 300 );

				$('#blood_insert_success').html( 'Blood details inserted Or updated successfully.' );
				$('#blood_success_display').css( 'display', 'block' );
				$('#blood_failed_display').css( 'display', 'none' );

				$("#wait").css("display","none");
				$('.col-lg-10').css("display","block");
			},
			error:function(){
				alert( "! Patient not found with provided information, Please check entered data of patient." );
			}
		});
	}

	function closeXray() {
		$('#blood_test').attr( 'checked', false );
		$('#blood_from' ). html( '' );
	}
</script>
<style>
ul#list-nav {
	list-style:none;
	margin-top:5px;
	margin-left:5px;
	padding:0;
	width: 94%;
}

ul#list-nav li {
	//display:inline;
	background-color:#F19102;
	color:#6A6B8F;
	font-size: 15px;
	margin-bottom: 10px;
	cursor:pointer;
}

ul#list-nav li a {
	text-decoration: none;
	padding: 5px 0;
	width: 100px;
	color: #eee;
	//float: left;
	text-align: center;
	border-bottom: 1px solid #fff;
}

ul#list-nav li:hover {
	background:#a2b3a1;
	color:#000;
}

td.min_height {
	line-height: 1;
	padding: 2px;
}

.pc_data {
	display: none;
}

textarea {
	resize: vertical;
}
</style>